package com.psa.tool;

import android.app.Activity;
import android.app.Application;
import com.wxw.BaseLibrary.net.NetSpeedTimer;

import java.util.LinkedList;
import java.util.List;

/**
 * Created by david on 14-3-5.
 */
public class ExitApplication extends Application {
    private List<Activity> activityList = new LinkedList<Activity>();

    private static ExitApplication exitApplication=null;
    private ExitApplication(){}

    public static ExitApplication getInstance(){
        if (exitApplication == null) {
            exitApplication = new ExitApplication();
        }
        return exitApplication;
    }

    public void addActivity(Activity activity){
        activityList.add(activity);
    }

    public void exit(){
        exit(1);
    }

    public void exit(int tag) {
        NetSpeedTimer.stopSpeed();
        RequestManager.getRequestManager().setRunning(false);
//        SystemStatus.notificationManager.cancelAll();
        for (Activity activity : activityList) {
            activity.finish();
        }
        if (DBTool.getInstance() != null) {
            DBTool.getInstance().close();
        }
        TrafficInfoProvider.logAppFlowCounterNow();
        if (tag>0) {
            System.exit(0);
        }
    }

    @Override
    public void onLowMemory() {
        super.onLowMemory();
        System.gc();
    }
}
